package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import com.sky.utils.QiniuUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @BelongsProject: sky-take-out
 * @BelongsPackage: com.sky.controller.admin
 * @Author: zhouzhijie
 * @CreateTime: 2023-11-13  11:32
 * @Description: 通用接口
 * @Version: 1.0
 */
@RestController
@RequestMapping("admin/common")
@Slf4j
@Api(tags = "通用接口")
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private QiniuUtils qiniuUtils;

    /**
     * @description: 文件上传接口
     * @author: zhouzhijie
     * @date: 2023/11/13 11:36
     * @param: [file]
     * @return: 
     **/
    @PostMapping("upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file){
        log.info("文件上传：{}",file);
        //上传到阿里云服务
        //文件原始名
        String originalFilename = file.getOriginalFilename();
        //截取原始文件名后缀：adfas.png
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //构建新的文件名
        String objectName = UUID.randomUUID().toString()+extension;
        //文件请求路径
        //String filePath = aliOssUtil.upload(file.getBytes(), objectName);
        String upload = qiniuUtils.upload(file, objectName);
        return Result.success(upload);


    }
}
